package project;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.text.*;
import java.sql.Connection;

public class AddEmpPanel extends JPanel implements ActionListener
{
	private JPanel formP = null, buttonP = null, westP = null,
				eastP = null, searchP = null, blankP = null;
	JButton saveB = null, searchB = null;
	JComboBox jcb = null;
	JLabel searchL = null;
	JTextField searchT = null;
	DetailsPanel dp;
	AddressPanel ap1;
	Connection conn;
	
	public AddEmpPanel(Connection conn)
	{
		this.conn = conn;
		this.setLayout(new BorderLayout());
		
		dp = new DetailsPanel("employee","add");
		ap1 = new AddressPanel("Residential");
		buttonP = new JPanel();
		saveB = new JButton(" Save ");
		saveB.addActionListener(this);
		buttonP.add(saveB);
		buttonP.setBorder(BorderFactory.createEtchedBorder());
		formP = new JPanel(new BorderLayout());
		formP.add(dp, BorderLayout.NORTH);
		formP.add(ap1, BorderLayout.CENTER);
		westP = new JPanel(new BorderLayout());
		westP.add(formP, BorderLayout.CENTER);
		westP.add(buttonP, BorderLayout.SOUTH);
		westP.setPreferredSize(new Dimension(280,450));
		westP.setBorder(BorderFactory.createEmptyBorder(0,1,0,0));
		
		eastP = new JPanel(new BorderLayout());
		eastP.setPreferredSize(new Dimension(500,450));
		searchP = new JPanel();
		searchL = new JLabel("Search: ");
		searchT = new JTextField(40);
		String [] jcbString = new String[]{
			"Employee ID", "Last Name", "Phone No"};
		jcb = new JComboBox(jcbString);
		searchB = new JButton("Search");
		searchP.add(searchL);
		searchP.add(searchT);
		searchP.add(jcb);
		searchP.add(searchB);
		//searchP.setBorder(BorderFactory.createCompoundBorder(
		//	BorderFactory.createEmptyBorder(10,10,10,10),
		//	BorderFactory.createRaisedBevelBorder()));
		searchP.setBorder(BorderFactory.createEtchedBorder());
		searchT.setEnabled(false);
		searchB.setEnabled(false);
		jcb.setEnabled(false);
		searchL.setEnabled(false);
		
		dp.accessLevelJCB.setEnabled(true);
		
		//PhotoPanel pp = new PhotoPanel();
		
		//eastP.add(searchP, BorderLayout.NORTH);
		//eastP.add(pp, BorderLayout.SOUTH);
		this.add(searchP, BorderLayout.NORTH);
		this.add(westP, BorderLayout.CENTER);
		this.add(eastP, BorderLayout.EAST);
		dp.empIDT.setFocusable(false);
		dp.empIDT.setBackground(new Color(173, 190, 230));
		
	}
	public void emptyAll()
	{
		dp.empIDT.setText("");
		dp.surnameT.setText("");
		dp.nameT.setText("");
		dp.initialsT.setText("");
		dp.idNumberT.setText("");
		dp.phoneOneT.setText("");
		dp.phoneTwoT.setText("");
		dp.emailT.setText("");
		dp.titleJCB.setSelectedItem("");
		dp.accessLevelJCB.setSelectedItem("");
		dp.jdc.setDate(null);
		ap1.streetT.setText("");
		ap1.suburbT.setText("");
		ap1.cityT.setText("");
		ap1.posCodeT.setText("");
		searchT.setText("");
	}
	public void actionPerformed(ActionEvent e)
	{
		Object source = e.getSource();
		if(source == saveB)
		{
			Date date = dp.jdc.getDate();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String bdate = sdf.format(date);
			Date now = new Date();
			String jdate = sdf.format(now);
			EmpManager cm = new EmpManager(conn);
			String surname = dp.surnameT.getText();
			String [] attr = new String [17];
			attr[0] = cm.generateEmpId(surname);
			attr[1] = cm.generateEmpPassword();
			attr[2] = dp.idNumberT.getText();
			attr[3] = dp.surnameT.getText();
			attr[4] = dp.nameT.getText();
			attr[5] = dp.initialsT.getText();
			attr[6] = (String)dp.titleJCB.getSelectedItem();
			attr[7] = ap1.streetT.getText();
			attr[8] = ap1.suburbT.getText();
			attr[9] = ap1.cityT.getText();
			attr[10] = ap1.posCodeT.getText();
			attr[11] = dp.phoneOneT.getText();
			attr[12] = dp.phoneTwoT.getText();
			attr[13] = dp.emailT.getText();
			attr[14] = bdate;
			attr[15] = jdate;
			attr[16] = (String)dp.accessLevelJCB.getSelectedItem();
			
			//for(int i = 0; i < attr.length; i++)
			//{
			//	System.out.println(attr[i]);
			//}
			
			
			Employee emp = new Employee(attr);
			cm.commitEmp(emp);
			String [] confirmArray = new String[4];
			confirmArray[0] = attr[0];
			confirmArray[1] = attr[4];
			confirmArray[2] = attr[3];
			confirmArray[3] = cm.getEmpPlainTextPassword();
			ConfirmDialog cd = new ConfirmDialog("Employee",confirmArray);
			emptyAll();
			
		}
	}
	

}